const query=require('../util/query')

const registry=async(params)=>{
const {username,password,nickname,phone}=params
let sql=`insert into user (username,password,nickname,phone) value ('${username}','${password}','${nickname}','${phone}')`
let res=await query(sql)
if(res.affectedRows==1){
 return {
    code:200,
    status:true,
    meg:'注册成功，请登录'
 }
}else{
    return {
    code:500,
    status:false,
    meg:'注册失败，请重试'
 }
}
}

const isreguser=async(params)=>{
  let sql=`select username from user where username='${params.username}'`
 let res=await query(sql)
 if(res.length>0){
  return{
    code:200,
    status:false,
    meg:'用户名已存在'
  }
 }
 else{
    return{
      code:403,
    status:true,
    meg:'用户名可以注册'
  }
 }
}

const login=async(params)=>{
const {username,password}=params
let sql=`select username from user where username='${username}' and password='${password}'`
let res=await query(sql)
if(res.length>0){
return{
  code:200,
  data:res,
}
}
else{
  return{
    code:403,
    data:null
  }
}
}

module.exports={
    isreguser,
    registry,
    login
}